package com.prs.crm.service.oa.impl;

import java.io.File;
import java.util.Collection;
import java.util.Date;

import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.AbstractDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.oa.JobPlanDao;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.JobPlan;
import com.prs.crm.domain.oa.JobPlanDetail;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.impl.BaseServiceImpl;
import com.prs.crm.service.oa.JobPlanDetailService;
import com.prs.crm.service.oa.JobPlanService;

public class JobPlanServiceImpl extends BaseServiceImpl<JobPlan,JobPlanDao> implements JobPlanService{
	private JobPlanDetailService jobPlanDetailService;
	private PersonService personService;
	
	@Override
	public void submitJobPlan(Collection<JobPlanDetail> jobPlanDetails,JobPlan jobPlan,String copyToPersons, File file, String fileName) {
		// TODO Auto-generated method stub
		Attachment attach = FileUtil.getInstance().createAttach(file, fileName);
		if(attach != null) {
			jobPlan.setAttach(attach);
		}
		if(copyToPersons != null) {
			Collection<Person> persons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(copyToPersons.split(",")));
			jobPlan.addCopyToPersons(persons);
		}
		
		this.getDao().save(jobPlan);
		
		for(JobPlanDetail detial : jobPlanDetails) {
			JobPlanDetail d = new JobPlanDetail();
			d.setJobPlan(jobPlan);
			d.setSummary(detial.getSummary());
			d.setState(Constrains.TASK_WAIT);
			
			this.getJobPlanDetailService().save(d);
		}
	}

	@Override
	public Collection<JobPlan> getLastJobPlan(String type, Person person,
			Date current) {
		// TODO Auto-generated method stub
		return this.getDao().getLastJobPlan(type, person, current);
	}

	public void setJobPlanDetailService(JobPlanDetailService jobPlanDetailService) {
		this.jobPlanDetailService = jobPlanDetailService;
	}

	public JobPlanDetailService getJobPlanDetailService() {
		return jobPlanDetailService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public PersonService getPersonService() {
		return personService;
	}

	@Override
	public Collection<JobPlan> getJobPlanByAudit(Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getJobPlanByAudit(person);
	}

	@Override
	public boolean existSameTimeJobPlan(Date startTime, Date endTime,
			String type,Person person) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Collection<JobPlan> jobPlans = this.getDao().getJobPlanByDate(startTime, endTime, type,person);
		if(jobPlans.size() != 0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public Collection<JobPlan> getJobPlanByKpi(Integer year, Integer month,
			String type, Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getJobPlanByKpi(year, month, type, person);
	}

	@Override
	public Collection<?> getReportJobPlanSource(String state, String type,
			Date datestart, Date dateend, Collection<Person> persons) {
		// TODO Auto-generated method stub
		return this.getDao().getReportJobPlanSource(state, type, datestart, dateend, persons);
	}

	@SuppressWarnings("unchecked")
	@Override
	public AbstractDataset fillDataset(Collection<?> dataList, int GraphicsType) {
		// TODO Auto-generated method stub
		AbstractDataset Dataset = null;
		if(GraphicsType == Constrains.REPORT_PIECHART)
			Dataset = new DefaultPieDataset();
		else if(GraphicsType == Constrains.REPORT_BARCHART)
			Dataset = new DefaultCategoryDataset();
		
		for(Object[] o : (Collection<Object[]>)dataList){
			String name = "";
			Number count = 0;
			for(Object value : o){
				if(value != null){
					if(value instanceof Person){
						name = ((Person) value).getResume().getName();
					}else if(value instanceof Dept){
						name = ((Dept) value).getName();
					}else if(value instanceof Number) {
						count = (Number)value;
					}
				}
			}
			if(!name.equals("")){
				if(GraphicsType == Constrains.REPORT_PIECHART)
					((DefaultPieDataset)Dataset).setValue(name, count);
				else if(GraphicsType == Constrains.REPORT_BARCHART)
					((DefaultCategoryDataset)Dataset).addValue(count, name, name);
			}
		}
		return Dataset;
	}

	@Override
	public Collection<?> getDeptReportJobPlanSource(Date datestart, Date dateend) {
		// TODO Auto-generated method stub
		return this.getDao().getDeptReportJobPlanSource(datestart, dateend);
	}

	@Override
	public Collection<JobPlan> getValidCritiqueJobPlan(Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getValidCritiqueJobPlan(person);
	}

}
